package com.tl.khadmin;


import com.alibaba.fastjson.JSON;
import com.tl.khadmin.bean.YiFanFrigthRegion;
import org.aspectj.apache.bcel.generic.RET;
import org.springframework.stereotype.Component;

import java.io.*;

import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import java.util.Map;

/**
 * @author lizb
 * @date 2021-08-22
 */
@Component
public class HaolibeiApi {



     public static String PostHaoliBeiProduct(Map<String, String> params) throws IOException {

         String url ="http://www.yifancq.com/open_api_notify.php";


             StringBuilder query = new StringBuilder();
             for (String key:params.keySet().toArray(new String[0])
                  ) {
                  query.append("&").append(key).append("=").append(params.get(key));
             }

         url=String.format("%S%s%s",url.toLowerCase(),"?method=notify.goods.update&format=json",query.toString());
         URL postUrl = new URL(url.toLowerCase());
         URLConnection conn = postUrl.openConnection();

         conn.setRequestProperty("accept", "*/*");
         conn.setRequestProperty("connection", "Keep-Alive");
         conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
         conn.setDoOutput(true);
         conn.setDoInput(true);

         String result = "";
         BufferedReader in = null;

         in = new BufferedReader(
                 new InputStreamReader(conn.getInputStream()));
         String line;
         while ((line = in.readLine()) != null) {
             result += line;
         }
         return  result;

     }


    public static String orderDeliverbillpost(Map<String, String> params) throws IOException {

        String url ="http://open.best-goods.com.cn/gjp_api_notify.php";

        StringBuilder query = new StringBuilder();
        for (String key:params.keySet().toArray(new String[0])
        ) {
            query.append("&").append(key).append("=").append(params.get(key));
        }

        url=String.format("%S%s%s",url.toLowerCase(),"?method=supplier.notify.sendorder",query.toString());
        URL postUrl = new URL(url.toLowerCase());
        URLConnection conn = postUrl.openConnection();

        conn.setRequestProperty("accept", "*/*");
        conn.setRequestProperty("connection", "Keep-Alive");
        conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        conn.setDoOutput(true);
        conn.setDoInput(true);

        String result = "";
        BufferedReader in = null;

        in = new BufferedReader(
                new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            result += line;
        }
        return  result;

    }

    public static String FreightRegionSync(YiFanFrigthRegion regon,boolean isDelete) throws IOException{
        String url ="http://www.yifancq.com/open_api_notify.php";
        url=isDelete?String.format("%S%s",url.toLowerCase(),"?method=notify.dlyregion.delete&format=json"):String.format("%S%s",url.toLowerCase(),"?method=notify.dlyregion.update&format=json");
        URL postUrl = new URL(url.toLowerCase());
        HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setUseCaches(false);
        connection.setInstanceFollowRedirects(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.connect();
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
        out.append(JSON.toJSONString(regon));
        out.flush();
        out.close();
        InputStream is =  connection.getInputStream();

        int length = connection.getContentLength();
        if (length != -1) {
            byte[] data = new byte[length];
            byte[] temp = new byte[512];
            int readLen = 0;
            int destPos = 0;
            while ((readLen = is.read(temp)) > 0) {
                System.arraycopy(temp, 0, data, destPos, readLen);
                destPos += readLen;
            }
            String result = new String(data, "UTF-8");
            System.out.println("主机返回:" + result);
            return  result;
        }
        return  "";

    }
}
